import 'dart:convert';
import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';

class HoxinUtil {
  static String identity = "";
  static DateUtil dateUtil = DateUtil();
  static TimerUtil timerUtil = TimerUtil();
  static String version = Platform.version;
  static String deviceId = md5
      .convert(const Utf8Encoder().convert(
          Platform.localHostname + Platform.version + Platform.operatingSystem))
      .toString();
  static String model =
      "${Platform.operatingSystem}:${Platform.operatingSystemVersion}";
  static Dio dio = Dio()
    // ..interceptors.add(InterceptorsWrapper(
    //   onRequest: (options, handler) {
    //     if (HoxinUtil.identity.isNotEmpty) {
    //       options.headers["identity"] = identity;
    //     }
    //   },
    //   onError: (e, handler) => print("error:${e.message}"),
    //   onResponse: (response, handler) {
    //     print("response:${response.requestOptions.path}");
    //   },
    // ))
    ..options.connectTimeout = 12000
    ..options.receiveTimeout = 12000;
  static Future<Map<String, dynamic>> httpGet(
    String url,
    dynamic data,
  ) async {
    var result = await dio.request(url,
        queryParameters: data, options: Options(method: 'GET'));
    return result.data as Map<String, dynamic>;
  }

  static Future<Map<String, dynamic>> httpPost(
    String url,
    dynamic data,
  ) async {
    var result =
        await dio.request(url, data: data, options: Options(method: 'POST'));
    return result.data as Map<String, dynamic>;
  }

  static Future<Map<String, dynamic>> httpDelete(
    String url,
    dynamic data,
  ) async {
    var result =
        await dio.request(url, data: data, options: Options(method: 'DELETE'));
    return result.data as Map<String, dynamic>;
  }

  static Future<Map<String, dynamic>> httpPut(
    String url,
    dynamic data,
  ) async {
    var result =
        await dio.request(url, data: data, options: Options(method: 'PUT'));
    return result.data as Map<String, dynamic>;
  }

  static Future<Map<String, dynamic>> httpPatch(
    String url,
    dynamic data,
  ) async {
    var result =
        await dio.request(url, data: data, options: Options(method: 'PATCH'));
    return result.data as Map<String, dynamic>;
  }
}
